#!/usr/local/bin/perl -w

use App::Options (
    options => [qw(dbdriver dbclass dbhost dbname dbuser dbpass)],
    option => {
        dbclass  => { default => "App::Repository::MySQL", },
        dbdriver => { default => "mysql", },
        dbhost   => { default => "localhost", },
        dbname   => { default => "test", },
        dbuser   => { default => "", },
        dbpass   => { default => "", },
    },
);

use Test::More qw(no_plan);
use lib "../App-Context/lib";
use lib "../../App-Context/lib";
use lib "lib";
use lib "../lib";
use lib ".";
use lib "t";

use App;
use App::Repository;
use RepositoryTestUtils qw(create_table_test_person drop_table_test_person populate_table_test_person);
use strict;

if (!$App::options{dbuser}) {
    ok(1, "No dbuser given. Tests assumed OK. (add dbuser=xxx and dbpass=yyy to app.conf in 't' directory)");
    exit(0);
}

my $context = App->context(
    conf_file => "",
    conf => {
        Repository => {
            default => {
                class => $App::options{dbclass},
                dbdriver => $App::options{dbdriver},
                dbhost => $App::options{dbhost},
                dbname => $App::options{dbname},
                dbuser => $App::options{dbuser},
                dbpass => $App::options{dbpass},
                table => {
                    test_person => {
                        primary_key => ["person_id"],
                    },
                },
            },
        },
    },
    debug_sql => $App::options{debug_sql},
    trace => $App::options{trace},
);

my $rep = $context->repository();
&drop_table_test_person($rep);
&create_table_test_person($rep);
&populate_table_test_person($rep);

my $dbtype = $App::options{dbtype};

###########################################################################
# DATA ACCESS TESTS
###########################################################################
my ($person_id, $first_name, $last_name, $address, $city, $state, $zip, $country);
my ($home_phone, $work_phone, $email_address, $gender, $birth_dt, $age);

my $columns = [ "person_id", "age", "first_name", "gender", "state" ];
my $rows = [
    [ 1, 39, "stephen",   "M", "GA", ],
    [ 2, 37, "susan",     "F", "GA", ],
    [ 3,  6, "maryalice", "F", "GA", ],
    [ 4,  3, "paul",      "M", "GA", ],
    [ 5,  1, "christine", "F", "GA", ],
    [ 6, 45, "tim",       "M", "FL", ],
    [ 7, 39, "keith",     "M", "GA", ],
];

my ($row, $nrows);

#####################################################################
#  $value  = $rep->get ($table, $key,     $col,   \%options);
#  $rep->set($table, $key,     $col,   $value,    \%options);
#####################################################################
$first_name = $rep->get("test_person", 1, "first_name");
is($first_name, "stephen", "starting: get() first_name [$first_name]");

$rep->begin_work();
is($rep->set("test_person", 1, "first_name", "steve"),1,"set() first name [steve]");
$first_name = $rep->get("test_person", 1, "first_name");
is($first_name, "steve",   "in transaction 1: get() (checking first_name is [$first_name])");

ok($rep->set("test_person", 2, ["first_name","age"], ["sue",38]), "set() 2 values");
($first_name, $age) = $rep->get("test_person", 2, ["first_name","age"]);
is($first_name, "sue",     "in transaction 1: get() 2 values (checking first_name is [$first_name])");
is($age,        38,        "in transaction 1: get() 2 values (checking age is [$age])");

$rep->rollback();
$first_name = $rep->get("test_person", 1, "first_name");
is($first_name, "stephen", "after rollback: get() (checking first_name is [$first_name])");
($first_name, $age) = $rep->get("test_person", 2, ["first_name","age"]);
is($first_name, "susan",   "after rollback: get() 2 values (checking first_name is [$first_name])");
is($age,        37,        "after rollback: get() 2 values (checking age is [$age])");

######################################################################################
# NOTE: Oracle commits on disconnect, so this test won't work        [']
######################################################################################
if ($dbtype eq "mysql") {
    $rep->begin_work();
    is($rep->set("test_person", 1, "first_name", "steve"),1,"set() first name [steve]");
    $first_name = $rep->get("test_person", 1, "first_name");
    is($first_name, "steve",   "in transaction 2: get() (checking first_name is [$first_name])");

    ok($rep->set("test_person", 2, ["first_name","age"], ["sue",38]), "set() 2 values");
    ($first_name, $age) = $rep->get("test_person", 2, ["first_name","age"]);
    is($first_name, "sue",     "in transaction 2: get() 2 values (checking first_name is [$first_name])");
    is($age,        38,        "in transaction 2: get() 2 values (checking age is [$age])");

    $rep->_disconnect();
    $first_name = $rep->get("test_person", 1, "first_name");
    is($first_name, "stephen", "after _disconnect: get() (checking first_name is [$first_name])");
    ($first_name, $age) = $rep->get("test_person", 2, ["first_name","age"]);
    is($first_name, "susan",   "after _disconnect: get() 2 values (checking first_name is [$first_name])");
    is($age,        37,        "after _disconnect: get() 2 values (checking age is [$age])");
}

$rep->begin_work();
is($rep->set("test_person", 1, "first_name", "steve"),1,"set() first name [steve]");
$first_name = $rep->get("test_person", 1, "first_name");
is($first_name, "steve",   "in transaction 3: get() (checking first_name is [$first_name])");

ok($rep->set("test_person", 2, ["first_name","age"], ["sue",38]), "set() 2 values");
($first_name, $age) = $rep->get("test_person", 2, ["first_name","age"]);
is($first_name, "sue",     "in transaction 3: get() 2 values (checking first_name is [$first_name])");
is($age,        38,        "in transaction 3: get() 2 values (checking age is [$age])");

$rep->commit();
$first_name = $rep->get("test_person", 1, "first_name");
is($first_name, "steve",   "after commit: get() (checking first_name is [$first_name])");
($first_name, $age) = $rep->get("test_person", 2, ["first_name","age"]);
is($first_name, "sue",     "after commit: get() 2 values (checking first_name is [$first_name])");
is($age,        38,        "after commit: get() 2 values (checking age is [$age])");

exit(0);

